﻿using System;
using System.Windows;
using System.Windows.Controls.Ribbon;
using BestCode.Creds.Entities;
using BestCode.Creds.UI.Report;
using BestCode.Creds.UI.ViewModel;

namespace BestCode.Creds.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class VwMain
    {

        public VwMain()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            LoadEntities();
        }


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            BestCode.Wpf.Controls.Basics.MessageDialog.ShowMe(this);
        }

        private void btnTemplates_Click(object sender, RoutedEventArgs e)
        {
            grdDynamic.Children.Clear();

            VwTemplate vwTemplate = new VwTemplate();
            vwTemplate.OnTemplateSelected += vwTemplate_OnTemplateSelected;
            grdDynamic.Children.Add(vwTemplate);

        }

        void vwTemplate_OnTemplateSelected(object sender, EventArgs e)
        {
            grdDynamic.Children.Clear();
            VwEditLayoutTemplate vwLayout = new VwEditLayoutTemplate();
            vwLayout.SetViewModel((Template)sender);
            grdDynamic.Children.Add(vwLayout);
        }

        public void LoadEntities()
        {
            grdDynamic.Children.Clear();

            VwEntities vwEntities = new VwEntities();
            var viewmodel = (VmEntity) vwEntities.DataContext;
            vwEntities.OnEntitySelected += vwEntities_OnEntitySelected;
            grdDynamic.Children.Add(vwEntities);
        }
        private void btnEntities_Click(object sender, RoutedEventArgs e)
        {
           
            LoadEntities();
        }

        void vwEntities_OnEntitySelected(object sender, EventArgs e)
        {
            grdDynamic.Children.Clear();
            VwFieldDefinition vwPersons = new VwFieldDefinition();
            vwPersons.SetViewModel((EntityInfo)sender);
            vwPersons.OnShowEnabledFieldsReport += vwPersons_onShowEnabledFieldsReport;
            grdDynamic.Children.Add(vwPersons);
        }

        void vwPersons_onShowEnabledFieldsReport(object sender, EventArgs e)
        {
            grdDynamic.Children.Clear();

            VwReport vwReport = new VwReport((EntityChild)sender);
            //vwReport.ShowEnabledItemsReport((EntityChild)sender);
            grdDynamic.Children.Add(vwReport);
        }
    }
}
